const BaseHandler = require('../base-handler');
const SystemServices = require('../../../constants').SystemServices;
const HTTP_VERBS = require('../../../constants').HTTP_VERBS;
const utils = require('../../../utils');


class LoginHandle extends BaseHandler {

    get needAuthenticated() {
        return false;
    }

    get pattern() {
        return "/login";
    }

    doGET(request, response) {
        var session = request.session;
        var security_code = utils.uuid();

        if (session && session.user && session.user.authenticated) {
            response.redirect('/admin/to/dingstore');
        } else {
            request.session.user.security_code = security_code;
            response.render('login', {
                success: true,
                security_code: security_code,
                data: {}
            });
        }
    }

    doPOST(request, response) {
        var me = this;

        var security_code = utils.uuid();
        if (!request.body.security_code || request.body.security_code !== request.session.user.security_code) {
            request.session.user.security_code = security_code;
            return response.render('login', {
                success: false,
                is_login: true,
                error: request.t('login.errors.invalid_request'),
                security_code: security_code,
                data: {}
            });
        }
        var username = (request.body.username || '').trim();
        var password = (request.body.password || '').trim();

        password = password.MD5(32);
        if (!username || !password) {
            request.session.user.security_code = security_code;
            response.render('login', {
                success: false,
                error: '请填写用户名和密码!',
                is_login: true,
                security_code: request.session.user.security_code,
                data: request.body
            });
        } else {
            this.Controller.verifyUser(username, password, (result) => {
                if (result.status) {
                    delete request.session.user.security_code
                    request.session.user.authenticated = true;
                    request.session.user.id = result.data.userId;
                    request.session.user.name = result.data.userName;
                    request.session.user.nickName = result.data.nickName;
                    request.session.user.email = result.data.email;
                    request.session.user.registDatetime = result.data.registDatetime;
                    request.session.user.headImage = result.data.headImage || '';
                    request.session.user.storeId = result.data.storeId || 1;
                    request.session.user.storeNo = result.data.storeNo ;
                    request.session.user.role = result.data.role;
                    request.session.user.roleIds = result.data.employeId;
                    response.redirect('/admin/to/active');
                } else {
                    result.error = request.t(result.error);
                    result.success = false;
                    result.is_login = true;
                    request.session.user.security_code = security_code;
                    result.security_code = request.session.user.security_code;
                    result.data = request.body;
                    response.render('login', result);
                }
            });
        }
    }

    setup() {
        this.registerVerb(HTTP_VERBS.GET, this.doGET);
        this.registerVerb(HTTP_VERBS.POST, this.doPOST);
    }
}

module.exports = LoginHandle;